using Framework.Save;
using LitJson;
using GameCore.UI;
using UnityEngine;

namespace GameCore
{
    public enum BaseInventoryState
    {
        Map, Warehouse, WorkTable, SmithTable, Loadout
    }
    
    public class PlayerBaseRegion : Region
    {
        public WarehouseInventory warehouseInventory;
        public PlayerInventory playerInventory;
        public CraftingInventory workTableInventory;
        public CraftingInventory smithTableInventory;
        public GameObject loadoutInventory;

        public SelectableIcon warehouseIcon;
        public SelectableIcon workTableIcon;
        public SelectableIcon smithTableIcon;
        public SelectableIcon loadoutIcon;

        public SelectableIcon currentSelectedIcon;
        public ClickableObject beginAdventureButton;

        public PanelAdventureMenu menu;
        
        private void Start()
        {
            warehouseIcon.OnSelected += ShowWarehous;
            workTableIcon.OnSelected += ShowWorkTable;
            smithTableIcon.OnSelected += ShowSmithTable;
            loadoutIcon.OnSelected += ShowLoadout;
            beginAdventureButton.OnClick += () =>
            {
                LevelManager.BeginAdventure("demo:核弹小镇");
            };
            loadoutIcon.Select();
        }

        private void OnEnable()
        {
            PlayerInputHandler.AddKeyDownListener(KeyCode.Escape, ToggleMenu);
        }

        private void OnDisable()
        {
            PlayerInputHandler.RemoveKeyDownListener(KeyCode.Escape, ToggleMenu);
        }

        private void ToggleMenu()
        {
            var active = !menu.gameObject.activeSelf;
            menu.gameObject.SetActive(active);
            if (active) PlayerInputHandler.Block(menu);
            else PlayerInputHandler.Unblock(menu);
        }
        
        private void HideAllInventories()
        {
            warehouseInventory.gameObject.SetActive(false);
            playerInventory.gameObject.SetActive(false);
            workTableInventory.gameObject.SetActive(false);
            smithTableInventory.gameObject.SetActive(false);
            loadoutInventory.SetActive(false);
            LevelManager.ClearInventory();
        }

        private void ShowWarehous()
        {
            HideAllInventories();
            warehouseInventory.gameObject.SetActive(true);
            playerInventory.gameObject.SetActive(true);
            currentSelectedIcon?.Deselect();
            currentSelectedIcon = warehouseIcon;
        }

        private void ShowWorkTable()
        {
            HideAllInventories();
            warehouseInventory.gameObject.SetActive(true);
            workTableInventory.gameObject.SetActive(true);
            currentSelectedIcon?.Deselect();
            currentSelectedIcon = workTableIcon;
        }

        private void ShowSmithTable()
        {
            HideAllInventories();
            warehouseInventory.gameObject.SetActive(true);
            smithTableInventory.gameObject.SetActive(true);
            currentSelectedIcon?.Deselect();
            currentSelectedIcon = smithTableIcon;
        }

        private void ShowLoadout()
        {
            HideAllInventories();
            warehouseInventory.gameObject.SetActive(true);
            loadoutInventory.SetActive(true);
            currentSelectedIcon?.Deselect();
            currentSelectedIcon = loadoutIcon;
        }
        
        public override JsonData Serialize()
        {
            var json = JsonUtil.NewObject();
            json["player_inventory"] = playerInventory.GetData().Serialize();
            json["warehouse"] = warehouseInventory.Serialize();
            json["work_table"] = workTableInventory.Serialize();
            json["smith_table"] = smithTableInventory.Serialize();
            json["loadout"] = "Not implemented yet";
            return json;
        }

        public override void Deserialize(JsonData data)
        {
            
        }
    }
}